#include "stdio.h"
#include "sgl_switch.h"
#include "../draw/sgl_draw_icon.h"
#include "../draw/sgl_draw_font.h"


int sgl_switch_init(sgl_switch_t *sw_btn, sgl_obj_t* parent, bool status)
{
    sgl_obj_t* obj;
    if(parent == NULL) {
        return -1;
    }
    sw_btn->status = status;
    obj = &sw_btn->obj;
    obj->event_data = NULL;
    obj->ev_stat = SGL_EVENT_FORCE_DRAW;
    obj->parent = parent;
    obj->obj_type = SGL_OBJ_SWITCH;
    sgl_obj_add_src_act_list(obj);
    sgl_obj_add_dirty_list(obj);
    return 0;
}

sgl_obj_t* sgl_switch_create(sgl_obj_t* parent, bool status)
{
    int ret;
    sgl_switch_t *sw_btn;
    if(parent == NULL) {
        return NULL;
    }
    sw_btn = (sgl_switch_t *)sgl_alloc(sizeof(sgl_switch_t));
    if(sw_btn == NULL)
        return NULL;
    
    ret = sgl_switch_init(sw_btn, parent, status);
    if(ret) {
        return NULL;
    }
    return  &sw_btn->obj;
}


void sgl_switch_set_toggle_color(sgl_obj_t *obj, sgl_color_t color)
{
    sgl_switch_t *sw = container_of(obj, sgl_switch_t, obj);
    sw->toggle_color = color;
}


void sgl_switch_draw(sgl_obj_t* obj)
{
    sgl_widget_draw_rect(rect);
    sgl_widget_buffer_valid(obj);
    sgl_switch_t *sw_btn = container_of(obj, sgl_switch_t, obj);
    sgl_surf_t *surf = sgl_draw_get_active_surf(obj);
    sgl_color_t dot_color;
    int toggle_w = obj->size.h - 2;
    sgl_widget_buffer_valid(obj);
    sgl_draw_buffer_clear(surf, obj->parent->style.body_color);
    if(obj->style.radius != 0) {
        if(sw_btn->status) {
            dot_color = sgl_color_mixer(obj->style.body_color, obj->parent->style.body_color, 128);
            sgl_draw_round_rect_solid(surf, rect, obj->style.radius, obj->style.body_color, obj->parent->style.body_color);
            rect.x1 = rect.x2 - toggle_w; 
            rect.x2 = rect.x2 - 2; 
            rect.y1 += 2; 
            rect.y2 -= 2;
            sgl_draw_round_rect_solid(surf, rect, obj->style.radius - 2, dot_color, obj->style.body_color);
        }
        else {
            dot_color = sgl_color_mixer(sw_btn->toggle_color, obj->parent->style.body_color, 128);
            sgl_draw_round_rect_solid(surf, rect, obj->style.radius, sw_btn->toggle_color, obj->parent->style.body_color);
            rect.x1 +=2; 
            rect.x2 = rect.x1 + toggle_w - 2; 
            rect.y1 += 2;
            rect.y2 -= 2;
            sgl_draw_round_rect_solid(surf, rect, obj->style.radius - 2, dot_color, sw_btn->toggle_color);
        }
    }
    else {
        if(sw_btn->status) {
            dot_color = sgl_color_mixer(obj->style.body_color, obj->parent->style.body_color, 128);
            sgl_draw_rect_solid(surf, rect, obj->style.body_color);
            rect.x1 = rect.x2 - toggle_w; 
            rect.x2 = rect.x2 - 2; 
            rect.y1 += 2; 
            rect.y2 -= 2;
            sgl_draw_rect_solid(surf, rect, dot_color);
        }
        else {
            dot_color = sgl_color_mixer(sw_btn->toggle_color, obj->parent->style.body_color, 128);
            sgl_draw_rect_solid(surf, rect, sw_btn->toggle_color);
            rect.x1 +=2; 
            rect.x2 = rect.x1 + toggle_w; 
            rect.y1 += 2; 
            rect.y2 -= 2;
            sgl_draw_rect_solid(surf, rect, dot_color);
        }
    }
    sgl_draw_obj_selected(obj, surf, rect);
    sgl_widget_draw_buffer_flush(obj, surf);
}

void sgl_switch_set_status(sgl_obj_t *obj, bool status)
{
    sgl_switch_t *sw_btn = container_of(obj, sgl_switch_t, obj);
    sw_btn->status = status;
}


bool sgl_switch_get_status(sgl_obj_t* obj)
{
    sgl_switch_t *sw_btn = container_of(obj, sgl_switch_t, obj);
    return sw_btn->status;
}

void sgl_switch_event_cb(sgl_obj_t *obj)
{
    if(obj->ev_stat == SGL_EVENT_PRESSED) {
        sgl_switch_t *sw_btn = container_of(obj, sgl_switch_t, obj);
        sw_btn->status = !sw_btn->status;
        sgl_obj_event_cb(obj);
        sgl_switch_draw(obj);
    }
    else if(obj->ev_stat == SGL_EVENT_FORCE_DRAW) {
        sgl_switch_draw(obj);
    }
}
